package com.hhkj.api.train.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.hhkj.admin.question.common.cont.QuestionTypeEnum;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.admin.train.common.cont.TrainConst;
import com.hhkj.admin.train.common.cont.TrainStudentAnswerConst;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.domain.TrainStudentAnswer;
import com.hhkj.admin.train.model.req.TrainReq;
import com.ruoyi.common.utils.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * CorrectUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/26 16:31
 */
public class CorrectUtils
{

    /**
     * 统计培训批改的答案信息
     *
     * @param trainReq 培训信息
     */
    public static void countTrainCorrectMark(TrainReq trainReq)
    {
        BigDecimal mark = BigDecimal.ZERO;
        for (QuestionRes questionGroup : trainReq.getQuestionGroupList()) {
            if (QuestionTypeEnum.CASE.getCode().equals(questionGroup.getTypeCode())) {
                if (CollectionUtil.isNotEmpty(questionGroup.getQuestionList())) {
                    for (QuestionRes question : questionGroup.getQuestionList()) {
                        countQuestionCorrectMark(question);
                        mark = mark.add(question.getStudentMark());
                    }
                }
            } else {
                countQuestionCorrectMark(questionGroup);
                mark = mark.add(questionGroup.getStudentMark());
            }
        }
        trainReq.setMark(mark);
    }

    /**
     * 统计 批改过题目的分数信息
     *
     * @param question 题目信息
     */
    private static void countQuestionCorrectMark(Question question)
    {
        BigDecimal studentMark = BigDecimal.ZERO;
        if (TrainStudentAnswerConst.CorrectRes.RIGHT.getCode().equals(question.getCorrectResCode())) {
            studentMark = question.getMark();
        }
        if (
                TrainStudentAnswerConst.CorrectRes.PART_RIGHT.getCode().equals(question.getCorrectResCode())
                        && question.isIncompleteHalfMarkFlag()
        ) {
            studentMark = question.getMark().divide(new BigDecimal(2), 2, RoundingMode.UP);
        }
        question.setStudentMark(studentMark);
    }

    /**
     * 从题目分组中获取对应得答案信息
     *
     * @param questionGroupList 题目组列表
     * @return 学生答案列表
     */
    public static List<TrainStudentAnswer> listStudentAnswerFromGroup(List<QuestionRes> questionGroupList)
    {
        List<TrainStudentAnswer> resultList = new ArrayList<>();
        int groupOrderNum = 0;
        for (QuestionRes questionGroup : questionGroupList) {
            resultList.add(new TrainStudentAnswer(questionGroup.getId(),
                    questionGroup.getStudentAnswer(),
                    questionGroup.getCorrectResCode(),
                    ++groupOrderNum,
                    questionGroup.getMark(),
                    questionGroup.getStudentMark(),
                    questionGroup.getActualOptionOrderList()));
            if (QuestionTypeEnum.CASE.getCode().equals(questionGroup.getTypeCode())) {
                for (Question question : questionGroup.getQuestionList()) {
                    resultList.add(new TrainStudentAnswer(question.getId(),
                            question.getStudentAnswer(),
                            question.getCorrectResCode(),
                            ++groupOrderNum,
                            question.getMark(),
                            question.getStudentMark(),
                            questionGroup.getActualOptionOrderList()));
                }
            }
        }
        return resultList;
    }

    /**
     * 统计培训题目信息中题目批改信息
     *
     * @param trainReq 培训信息
     */
    public static void countTrainQuestionCorrect(TrainReq trainReq)
    {
        if (CollectionUtil.isEmpty(trainReq.getQuestionGroupList())) {
            return;
        }
        trainReq.setAnswerAmount(0);
        trainReq.setRightAmount(0);
        trainReq.setErrorAmount(0);
        for (QuestionRes questionGroup : trainReq.getQuestionGroupList()) {
            if (QuestionTypeEnum.CASE.getCode().equals(questionGroup.getTypeCode())) {
                for (QuestionRes question : questionGroup.getQuestionList()) {
                    countQuestionCorrect(question, trainReq);
                }
            } else {
                countQuestionCorrect(questionGroup, trainReq);
            }
        }
    }

    /**
     * 统计题目批改结果信息
     *
     * @param question 题目信息
     * @param train    培训信息
     */
    private static void countQuestionCorrect(Question question, Train train)
    {
        if (StrUtil.isNotBlank(question.getStudentAnswer())) {
            train.setAnswerAmount(train.getAnswerAmount() + 1);
        }
        if (StrUtil.isBlank(question.getCorrectResCode())
                || TrainStudentAnswerConst.CorrectRes.ERROR.getCode().equals(question.getCorrectResCode())
                || TrainStudentAnswerConst.CorrectRes.PART_RIGHT.getCode().equals(question.getCorrectResCode())
        ) {
            train.setErrorAmount(train.getErrorAmount() + 1);
            return;
        }
        // 未作答的情况，如果是练习 不算错题目，如果是测试算错题
        if (TrainStudentAnswerConst.CorrectRes.UNANSWERED.getCode().equals(question.getCorrectResCode()) &&
                TrainConst.Type.TEST.getCode().equals(train.getTypeCode())) {
            train.setErrorAmount(train.getErrorAmount() + 1);
            return;
        }
        if (TrainStudentAnswerConst.CorrectRes.RIGHT.getCode().equals(question.getCorrectResCode())) {
            train.setRightAmount(train.getRightAmount() + 1);
        }
    }

    /**
     * 批改题目信息
     *
     * @param question      题目
     * @param studentAnswer 答案
     */
    public static void correctQuestion(Question question, String studentAnswer)
    {
        studentAnswer = studentAnswer.trim();
        String correctResCode = TrainStudentAnswerConst.CorrectRes.UNANSWERED.getCode();
        if (StrUtil.isNotBlank(studentAnswer)) {
            // 单选判断 答案相等为对，其余为错
            if (QuestionTypeEnum.SINGLE.getCode().equals(question.getTypeCode())
                    || QuestionTypeEnum.JUDGE.getCode().equals(question.getTypeCode())) {
                if (question.getAnswer().equals(studentAnswer)) {
                    correctResCode = TrainStudentAnswerConst.CorrectRes.RIGHT.getCode();
                } else {
                    correctResCode = TrainStudentAnswerConst.CorrectRes.ERROR.getCode();
                }
            }
            // 多选题 题目答案包含学生答案信息（长度一样全对，长度不一致部分正确） 其余错误
            if (QuestionTypeEnum.MULTIPLE.getCode().equals(question.getTypeCode())) {
                Set<String> questionAnswerSet = StringUtils.strChar2Set(question.getAnswer());
                Set<String> studentAnswerSet = StringUtils.strChar2Set(studentAnswer);
                if (questionAnswerSet.containsAll(studentAnswerSet)) {
                    if (questionAnswerSet.size() == studentAnswerSet.size()) {
                        correctResCode = TrainStudentAnswerConst.CorrectRes.RIGHT.getCode();
                    } else {
                        correctResCode = TrainStudentAnswerConst.CorrectRes.PART_RIGHT.getCode();
                    }
                } else {
                    correctResCode = TrainStudentAnswerConst.CorrectRes.ERROR.getCode();
                }
            }
        }
        question.setStudentAnswer(studentAnswer);
        question.setCorrectResCode(correctResCode);
    }
}
